package com.gr100.biz.api.user;

import com.gr100.biz.api.bo.MetaObject;
import com.gr100.biz.api.user.hex.AbsUserHex;
import com.gr100.biz.api.user.hex.UserExistsHex;
import com.gr100.biz.api.user.hex.UserSystemHex;
import com.gr100.biz.api.user.hex.ValidateHex;
import com.jfinal.log.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by YFsoft on 16/3/14.
 */
public class UserService {
    private static final Log logger = Log.getLog(UserService.class);
    /**
     * 用户注册
     * @param type
     * @param loginName
     * @param loginPass
     * @param cfgPass
     * @return
     */
    public Map signup(String type,String loginName,String loginPass,String cfgPass,String unionID){

        Map<String,Object> map = new HashMap<String,Object>();

        User user = new User(type,loginName,loginPass,cfgPass,unionID);
        try {
            //非空验证
            user.validate();

            //验证用户是否已经存在
            if(user.checkExists()){
                throw new UserExistsHex(-607,"LOGINNAME CANNOT BE REPEATED REGISTRATION","用户名不可重复注册");
            }

            //保存用户
            int uid = user.signIn();
            if(uid > 0) {
                Map<String, Object> p = new HashMap<String, Object>();
                p.put("loginName", loginName);
                p.put("uid", uid);
                map.put("data", p);
            }else{
                throw new UserSystemHex(-608,"Network anomaly","网络异常,请稍候再试");
            }

        } catch (AbsUserHex userHex) {
            map.put("errno", userHex.errno);
            map.put("code", userHex.code);
            map.put("msg", userHex.message);
        }
        return map;
    }

    /**
     * 用户登录
     * @param type
     * @param loginName
     * @param loginPass
     * @return
     */
    public Map signin(String type,String loginName,String loginPass,String unionID){

        Map<String,Object> map = new HashMap<String,Object>();
        User user = new User(type,loginName,loginPass,loginPass,unionID);
        try {
            //非空验证
            user.validate();

            //验证用户是否已经存在
            MetaObject u =  user.signin();
            logger.info("=======================>u:" + u);
            if(u == null){
                logger.info("=======================>type:" + type);
                if("3".equals(type)){
                    int uid = user.signIn();
                    logger.info("=======================>uid:" + uid);
                    if(uid == 0) {
                        throw new UserSystemHex(-608,"Network anomaly","网络异常,请稍候再试");
                    }else{
                        u =  user.signin();
                        logger.info("=======================>u:" + u);
                    }
                }else{
                    throw new UserExistsHex(-609,"Username or password is wrong","用户名或密码有误");
                }
            }

            logger.info("Logger from UserService: u.getColumns:" + u.getColumns());
            map.put("data", u.getColumns());

        } catch (AbsUserHex userHex) {
            map.put("errno", userHex.errno);
            map.put("code", userHex.code);
            map.put("msg", userHex.message);
        }
        return map;
    }

    /**
     * 获取用户信息
     * @param type
     * @param loginName
     * @param loginPass
     * @return
     */
    public Map getUser(String type,String loginName,String loginPass){

        Map<String,Object> map = new HashMap<String,Object>();

        String unionID = "";
        User user = new User(type,loginName,loginPass,loginPass,unionID);
        try {
            //非空验证
            user.validate();

            //验证用户是否已经存在
            MetaObject u =  user.signin();
            if(u == null){
                throw new UserExistsHex(-609,"Username or password is wrong","用户名或密码有误");
            }

            map.put("data", u);

        } catch (AbsUserHex userHex) {
            map.put("errno", userHex.errno);
            map.put("code", userHex.code);
            map.put("msg", userHex.message);
        }
        return map;
    }
}
